home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Textures SDK / makels / makels.cpp next >
Text File  |  1998-12-08  |  4KB  |  173 lines

  1. /***
  2. *
  3. *    Copyright (c) 1998, Valve LLC. All rights reserved.
  4. *    
  5. *    This product contains software technology licensed from Id 
  6. *    Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
  7. *    All Rights Reserved.
  8. *
  9. ****/
  10.  
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16.  
  17. char    **ppszFiles = NULL;
  18. int        nFiles = 0;
  19. int        nMaxFiles = 0;
  20.  
  21. int
  22. string_comparator( const void *string1, const void *string2 )
  23. {
  24.     char    *s1 = *(char **)string1;
  25.     char    *s2 = *(char **)string2;
  26.     return strcmp( s1, s2 );
  27. }
  28.  
  29. void PrintUsage(char *pname)
  30. {
  31.     printf("\n\tusage:%s <source directory> <wadfile name> <script name> \n\n",pname);
  32.     printf("\t%s.exe is used to generate a bitmap name sorted 'qlumpy script'.\n",pname);
  33. }
  34.  
  35. int main(int argc, void **argv)
  36. {
  37.     char *pszdir;
  38.     char *pszWadName;
  39.     char *pszScriptName;
  40.     char szBuf[1024];
  41.     HANDLE hFile, hScriptFile;
  42.     WIN32_FIND_DATA FindData;
  43.     BOOL fWrite;
  44.     BOOL fContinue = TRUE;
  45.     DWORD dwWritten;
  46.  
  47.     printf("makels Copyright (c) 1998 Valve L.L.C., %s\n", __DATE__ );
  48.  
  49.     pszdir = (char *)argv[1];
  50.  
  51.     if ((argc != 4) || (pszdir[0] == '/') || (pszdir[0] == '-'))
  52.     {
  53.         PrintUsage((char *)argv[0]);
  54.         exit(1);
  55.     }
  56.  
  57.     pszdir = (char *)malloc(strlen((char *)argv[1]) + 7);
  58.     strcpy(pszdir, (char *)argv[1]);
  59.     strcat(pszdir, "\\*.bmp");
  60.  
  61.     pszWadName = (char *)malloc(strlen((char *)argv[2]) + 5);
  62.     strcpy(pszWadName, (char *)argv[2]);
  63.     strcat(pszWadName, ".WAD");
  64.  
  65.     pszScriptName = (char *)malloc(strlen((char *)argv[3]));
  66.     strcpy(pszScriptName, (char *)argv[3]);
  67.     hScriptFile = CreateFile(pszScriptName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 
  68.             FILE_ATTRIBUTE_NORMAL, NULL);
  69.  
  70.     if (hScriptFile == INVALID_HANDLE_VALUE)
  71.     {
  72.         printf("\n---------- ERROR ------------------\n");
  73.         printf(" Could not open the script file: %s\n", pszScriptName);
  74.         Beep(800,500);
  75.         exit(EXIT_FAILURE);
  76.     }
  77.  
  78.     sprintf(szBuf, "$DEST    \"%s\"\r\n\r\n", pszWadName);
  79.     fWrite = WriteFile(hScriptFile, szBuf, strlen(szBuf), &dwWritten, NULL);
  80.     if (!fWrite || (dwWritten != strlen(szBuf)))
  81.     {
  82. write_error:
  83.         printf("\n---------- ERROR ------------------\n");
  84.         printf(" Could not write to the script file: %s\n", pszScriptName);
  85.         Beep(800,500);
  86.         CloseHandle(hScriptFile);
  87.         exit(EXIT_FAILURE);
  88.     }
  89.     
  90.     
  91.     hFile = FindFirstFile(pszdir, &FindData);
  92.  
  93.     if (hFile != INVALID_HANDLE_VALUE)
  94.     {
  95.         while (fContinue)
  96.         {
  97.             if (!(FindData.dwFileAttributes &
  98.                     (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_HIDDEN)))
  99.             {
  100.                 char szShort[MAX_PATH];
  101.  
  102.                 // ignore N_ and F_ files
  103.                 strcpy(szShort, FindData.cFileName);
  104.                 strupr(szShort);
  105.  
  106.                 if ((szShort[1] == '_') && ((szShort[0] == 'N') || (szShort[0] == 'F')))
  107.                 {
  108.  
  109.                     printf("Skipping %s.\n", FindData.cFileName);
  110.  
  111.                 } else {
  112.                 
  113.                     if ( nFiles >= nMaxFiles )
  114.                     {
  115.                         nMaxFiles += 1000;
  116.                         ppszFiles = (char **)realloc( ppszFiles, nMaxFiles * sizeof(*ppszFiles) );
  117.                         if ( !ppszFiles )
  118.                         {
  119.                             printf("\n---------- ERROR ------------------\n");
  120.                             printf(" Could not realloc more filename pointer storage\n");
  121.                             Beep(800,500);
  122.                             exit(EXIT_FAILURE);
  123.                         }
  124.                     }
  125.                     ppszFiles[nFiles++] = strdup( szShort );
  126.                 }
  127.             }
  128.             fContinue = FindNextFile(hFile, &FindData);
  129.         }    
  130.     }
  131.  
  132.  
  133.     if (nFiles > 0)
  134.     {
  135.         qsort( ppszFiles, nFiles, sizeof(char*), string_comparator );
  136.  
  137.         for( int i = 0; i < nFiles; i++ )
  138.         {
  139.             char *p;
  140.             char szShort[MAX_PATH];
  141.             char szFull[MAX_PATH];
  142.  
  143.             strcpy(szShort, pszdir);
  144.             p = strchr(szShort, '*');
  145.             *p = '\0';
  146.             strcat(szShort, ppszFiles[i]);
  147.             GetFullPathName(szShort, MAX_PATH, szFull, NULL);
  148.  
  149.             sprintf(szBuf, "$loadbmp    \"%s\"\r\n", szFull);
  150.             fWrite = WriteFile(hScriptFile, szBuf, strlen(szBuf), &dwWritten, NULL);
  151.             if (!fWrite || (dwWritten != strlen(szBuf)))
  152.                 goto write_error;
  153.  
  154.  
  155.             p = strchr(ppszFiles[i], '.');
  156.             *p = '\0';
  157.  
  158.             sprintf(szBuf, "%s  miptex -1 -1 -1 -1\r\n\r\n", ppszFiles[i]);
  159.             fWrite = WriteFile(hScriptFile, szBuf, strlen(szBuf), &dwWritten, NULL);
  160.             if (!fWrite || (dwWritten != strlen(szBuf)))
  161.                 goto write_error;
  162.  
  163.             free( ppszFiles[i] );
  164.         }
  165.     }
  166.     
  167.     printf("Processed %d files specified by %s\n", nFiles, pszdir );
  168.  
  169.     CloseHandle(hScriptFile);
  170.     free(pszdir);
  171.     exit(0);
  172.     return 0;
  173. }